package com.huazi.www.doctor.hu.dao.impl;


import com.huazi.www.doctor.hu.dao.DoctorDao;
import com.huazi.www.doctor.hu.entity.*;
import com.huazi.www.doctor.hu.util.BaseDao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DoctorDaoImpl extends BaseDao implements DoctorDao {
    /**
     * 查询候诊中的总人数
     *
     * @return
     */
    @Override
    public int totalCount() {
        String sql = "select count(1) from doctor_queue where state=1";//查询候诊中的总人数
        int count = 0;
        rs = executeQuery(sql);
        try {
            while (rs.next()) {
                count = rs.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(rs, ps, conn);
        }
        return count;
    }

    /**
     * 查询是否有该病人记录
     *
     * @param no
     * @return
     */
    @Override
    public int selCtCount(Integer no) {
        String sql = "select count(CdiagnoseNO) from department_ct where CdiagnoseNO=?";
        rs = executeQuery(sql, no);
        int count = 0;
        try {
            while (rs.next()) {
                count = rs.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(rs, ps, conn);
        }
        return count;
    }

    /**
     * 查询是否有以往病例
     *
     * @param no
     * @return
     */
    @Override
    public int selDiCount(Integer no) {
        String sql = "select count(userid) from doctor_diagnose where userid=?";
        rs = executeQuery(sql, no);
        int count = 0;
        try {
            while (rs.next()) {
                count = rs.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(rs, ps, conn);
        }
        return count;
    }

    /**
     * 查询以往病例
     *
     * @param no
     * @return
     */
    @Override
    public List<DiseaseInfo> selDisease(Integer no) {
        List<DiseaseInfo> list = new ArrayList<>();
        String sql = "select usertype,diagnosedate from doctor_diagnose where userid=? ORDER BY `diagnosedate` DESC ";
        rs = executeQuery(sql, no);
        try {
            while (rs.next()) {
                String usertype = rs.getString(1);
                String diagnosedate = rs.getString(2);
                DiseaseInfo diseaseInfo = new DiseaseInfo(usertype, diagnosedate);
                list.add(diseaseInfo);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(rs, ps, conn);
        }
        return list;
    }

    /**
     * 显示候诊中的病人
     *
     * @param start
     * @param pageSize
     * @return
     */
    @Override
    public List<PatientInfo> selPat(Integer start, Integer pageSize, Integer doctorId) {
        List<PatientInfo> list = new ArrayList<>();
        String sql = "select id,patientId,patientName,doctorid,time,state from doctor_queue where state=1 and doctorid=? order by time desc  limit ?,?";
        rs = executeQuery(sql, doctorId, start, pageSize);
        try {
            while (rs.next()) {
                int id = rs.getInt(1);//编号
                int patientId = rs.getInt(2);//病人编号
                String patientName = rs.getString(3);//病人姓名
                int doctorid = rs.getInt(4);//医生编号
                String time = rs.getString(5);//问诊时间
                int state = rs.getInt(6);//状态
                PatientInfo patientInfo = new PatientInfo(id, patientId, patientName, doctorid, time, state);
                list.add(patientInfo);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(rs, ps, conn);
        }
        return list;
    }

    /**
     * 查询病人CT信息
     *
     * @param no
     * @return
     */
    @Override
    public List<CtInfo> selCt(Integer no) {
        List<CtInfo> list = new ArrayList<>();
        String sql = "select ct.id,ct.CdiagnoseNO,ct.CuserName,ct.CTime,ct.inspectionItem,ct.diagnose,ct.doctorid from department_ct as ct,doctor_queue as qu where ct.CdiagnoseNO=qu.patientId and ct.CdiagnoseNO=? and qu.state=1  ORDER BY ct.`CTime` DESC";
        rs = executeQuery(sql, no);
        try {
            while (rs.next()) {
                int id = rs.getInt(1);//ct检查编号
                int CdiagnoseNO = rs.getInt(2);//诊断号
                String name = rs.getString(3);//患者姓名
                String time = rs.getString(4);//诊断时间
                String inspectionItem = rs.getString(5);//检查项目
                String diagnose = rs.getString(6);//检查结
                int doctorid = rs.getInt(7);//医生id
                CtInfo ct = new CtInfo(id, CdiagnoseNO, name, time, inspectionItem, diagnose, doctorid);
                list.add(ct);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(rs, ps, conn);
        }
        return list;
    }

    /**
     * 得到药单的集合
     *
     * @return
     */
    @Override
    public List<Drug> listdrug() {
        String sql = "SELECT id,drugname,count from drug_remedy";
        List<Drug> list = new ArrayList<>();
        ResultSet rs = executeQuery(sql);
        try {
            while (rs.next()) {
                Drug d = new Drug();
                d.setDrugid(rs.getInt(1));
                d.setDrugname(rs.getString(2));
                d.setCount(rs.getInt(3));
                list.add(d);

            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return list;
    }

    /**
     * 医生开药
     *
     * @param list
     * @param bingli
     */
    public void druglist(List<Medicine> list, String bingli) {
        String sql = "INSERT `doctor_medicine` (`userid`,`username`,`medicineid`,`sum`,`time`,`doctorid`,`type`)\n" +
                "VALUES (?,?,?,?,?,?,?)";
        for (int i = 0; i < list.size(); i++) {
            getUpdate(sql, list.get(i).getUserid(), list.get(i).getUsername(), list.get(i).getMedicineid(), list.get(i).getSum(), list.get(i).getTime(), list.get(i).getDoctorid(), list.get(i).getType()
            );

        }

        //插入病例
        String sts = "insert doctor_diagnose(userid,usertype,diagnosedate,doctorid)values(?,?,?,?)";
        getUpdate(sts, list.get(0).getUserid(), bingli, list.get(0).getTime(), list.get(0).getDoctorid());
        //修改问政状态
        String str = "UPDATE `doctor_queue` SET `state`=2 WHERE `patientId`=?";
        getUpdate(str, list.get(0).getUserid());


    }

    /**
     * 排队列表的查询判断
     *
     * @param id
     * @return
     */
    @Override
    public int usercheckselect(int id) {

        int count = 0;
        String sql = "select count(1) FROM department_queue where userId =? AND state  IN(0,1,2)";
        ResultSet rs = executeQuery(sql, id);
        try {
            rs.next();
            count = rs.getInt(1);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        //插入排队记录
        if (count == 0) {
            String insertsql = "insert  department_queue (userId,userType,userMeney) values (?,'CT',300)";
            getUpdate(insertsql, id);
        }
        return count;
    }
}
